﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;
using WebGrease.Css.Extensions;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using CanDoo.Core.Data;
using FineUIPro;

namespace CanDoo.Oil.WebApp.financeSellStatement
{
    public partial class Default : ListPageBase<ISellStatementContract, SellStatement>
    {

        public override bool IsMaxMode
        {
            get
            {
                return true;
            }
        }
        public ICustomerContract CustomerContract { get; set; }
        public IPayTypeContract PayTypeContract { get; set; }
        public IUserContract UserContract { get; set; }
        public ISellAdvanceMoneyContract SellAdvanceMoneyContract { get; set; }

        #region 方法
        protected override void InitGrid()
        {
            Selector = p => new
            {
                p.Code,
                p.Customer,
                p.CustomerId,
                p.PayDate,
                p.PayType,
                p.PayTypeId,
                p.User,
                p.UserId,
                p.SellAdvanceMoney,
                p.SellAdvanceMoneyId,
                p.AdvanceMoney,
                p.Remark,
                p.PassState,
                p.PassAutitedTime,
                p.PassAutitedUserId,
                p.Id,
                p.Name
            };
            base.InitGrid();
        }

        protected override void Search()
        {
            var key = SearchKey.GetValue();
            var customerId = 0L;
            if (SearchCustomer.Values.Count() > 0 && SearchCustomer.Values[0].ToLong() >  0L)
                 customerId = SearchCustomer.Values[0].ToLong();
            var paytypeId = SearchPayType.GetValue().ToLong();
            var userId = 0L;
            if (SearchUser.Values.Count() > 0 && SearchUser.Values[0].ToLong() >  0L)
                 userId = SearchUser.Values[0].ToLong();
            var selladvancemoneyId = 0L;
            if (SearchSellAdvanceMoney.Values.Count() > 0 && SearchSellAdvanceMoney.Values[0].ToLong() >  0L)
                 selladvancemoneyId = SearchSellAdvanceMoney.Values[0].ToLong();
            if(string.IsNullOrEmpty(key) == false)
            {
                Filter = Filter.And(p => p.Name.Contains(key));
            }
            if (customerId >  0L)
                Filter = Filter.And(p => p.CustomerId == customerId);
            if (paytypeId >  0L)
                Filter = Filter.And(p => p.PayTypeId == paytypeId);
            if (userId >  0L)
                Filter = Filter.And(p => p.UserId == userId);
            if (selladvancemoneyId >  0L)
                Filter = Filter.And(p => p.SellAdvanceMoneyId == selladvancemoneyId);

            var isDeleted = ManageRecycle.Checked;
            Filter = Filter.And(p => p.IsDeleted == isDeleted);

            if (_SearchIsPassed != null && _SearchIsPassed.GetValue() != "-1")
            {
                PassState passstate = PassState.Wait;
                if(Enum.TryParse(_SearchIsPassed.GetValue(),out passstate))
                {
                    Filter = Filter.And(p => p.PassState == passstate);
                }
            }
        }
        #endregion

        #region 事件
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //FineUIListHelper.BindDDL<Customer>("客户",CustomerContract.GetResolvedTree(), 0L, SearchCustomer);
                //FineUIListHelper.BindDDL(CustomerContract.EntityName, this.CustomerContract.GetSelectList(), 0L, SearchCustomer);
                //FineUIListHelper.BindDDL<PayType>("收款方式",PayTypeContract.GetResolvedTree(), 0L, SearchPayType);
                FineUIListHelper.BindDDL(PayTypeContract.EntityName, this.PayTypeContract.GetSelectList(), 0L, SearchPayType);
                //FineUIListHelper.BindDDL<User>("经办人",UserContract.GetResolvedTree(), 0L, SearchUser);
                //FineUIListHelper.BindDDL(UserContract.EntityName, this.UserContract.GetSelectList(), 0L, SearchUser);
                //FineUIListHelper.BindDDL<SellAdvanceMoney>("预收款",SellAdvanceMoneyContract.GetResolvedTree(), 0L, SearchSellAdvanceMoney);
                //FineUIListHelper.BindDDL(SellAdvanceMoneyContract.EntityName, this.SellAdvanceMoneyContract.GetSelectList(), 0L, SearchSellAdvanceMoney);
                InitGrid();
            }
        }
        #endregion

        #endregion 事件

    }
}
